//
//  main.cpp
//  opencv_project
//
//  Created by 黄琛 on 2018/12/25.
//  Copyright © 2018 黄琛. All rights reserved.
//

#include "opencv2/opencv.hpp"

using namespace cv;

int main(void)
{
    // First we declare the variables we are going to use
    Mat src, rs, gray;
    Mat grad_x, grad_y;
    Mat abs_grad_x, abs_grad_y, grad, detected_edges;
    
    src = imread("Lena.jpg");
    
    //缩小图像
    pyrDown(src, rs);
    
    //转换为灰度图
    cvtColor(rs, gray, CV_RGB2GRAY);
    
    //Sobel算子边缘检测
    Sobel(gray, grad_x, CV_16S, 1, 0);
    Sobel(gray, grad_y, CV_16S, 0, 1);
    // converting back to CV_8U
    convertScaleAbs(grad_x, abs_grad_x);
    convertScaleAbs(grad_y, abs_grad_y);
    //approximate the gradient by adding both directional gradients
    addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
    
    //Canny算子边缘检测
    Canny(gray, detected_edges, 45, 135);
    
    //show results
    imshow("Sobel result", grad);
    imshow("Canny result", detected_edges);
    waitKey(0);
    
    return 0;
}
